package com.wb.oldwomen;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Process;
import android.view.KeyEvent;
import android.view.MotionEvent;

import cn.cmgame.billing.api.GameInterface;
import cn.cmgame.billing.api.GameInterface.GameExitCallback;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.IConstants;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.TargetSelector;

public class GameScene extends Scene implements IConstants {
	public int mGameStatue = GS_GAME_INIT;

	public GamePlayLayer mGamePlayLayer;
	UIMainMenu mmainMenu;
	UIIGM mIGM;
	UIResult mResult;
	UILevelSelect mLevelSelect;
	UIHelp mHelp;
	UILevelConfirm mLevelConfirm;
	UIShop mshop;
	
	public boolean mIsGameStart = false;

	public int mDelayShowTime = 1000;

	public static final int FONT_SIZE = 35;

	public GameScene() {
		Global.sWYSize = Director.getInstance().getWindowSize();
		init();

		WYRect clip = WYRect.make(Global.sStartX, Global.sEndY, Global.sEndX
				- Global.sStartX, Global.sStartY - Global.sEndY);
		setClipRect(clip);
		setTouchEnabled(true);
		setMultiTouchClickable(false);
		setKeyEnabled(true);
		schedule(new TargetSelector(this, "tick(float)", new Object[] { 0f }));
		autoRelease(true);

//		loadLockedInfo();
		// SoundManager.playMusic(0);
	}

	

	public int getCurLevel() {
		if (mLevelSelect != null)
			return mLevelSelect.getCurLevel();
		return 0;
	}

	public void init() {
		// 主菜单
		if (mmainMenu == null) {
			mmainMenu = UIMainMenu.make(this);
			mmainMenu.setVisible(false);
			addChild(mmainMenu);
		}

		// 帮助
		if (mHelp == null) {
			mHelp = UIHelp.make(this);
			mHelp.setVisible(false);
			addChild(mHelp);
		}

		// 大关选择
		if (mLevelSelect == null) {
			mLevelSelect = UILevelSelect.make(this);
			mLevelSelect.setVisible(false);
			addChild(mLevelSelect);
		}
		
		if (mLevelConfirm == null) {
			mLevelConfirm = UILevelConfirm.make(this);
			mLevelConfirm.setVisible(false);
			addChild(mLevelConfirm);
		}
		// //小关选择
		// if (mLevelSubSelect == null) {
		// mLevelSubSelect = UILevelSubSelect.make(this);
		// mLevelSubSelect.setVisible(false);
		// addChild(mLevelSubSelect);
		// }

		if (mGamePlayLayer == null) {
			mGamePlayLayer = GamePlayLayer.make(this);
			mGamePlayLayer.setVisible(false);
			addChild(mGamePlayLayer);
		}
		
		//shop
		if (mshop == null) {
			mshop = UIShop.make(this);
			mshop.setVisible(false);
			addChild(mshop);
		}

		// igm
		if (mIGM == null) {
			mIGM = UIIGM.make(this);
			mIGM.setVisible(false);
			addChild(mIGM);
		}
		
		if (mResult == null) {
			mResult = UIResult.make(this);
			mResult.setVisible(false);
			addChild(mResult);
			mGamePlayLayer.setResultListener(mResult);
		}
		
		switchState(GS_GAME_MAIN_MENU);
		// switchState(GS_GAME_RUNNING);

	}

	public boolean isIGM() {
		return mGameStatue == GS_GAME_IGM;
	}

	public boolean mPause;

	public void xlPause() {
		mPause = true;
	}

	public void xlResume() {
		mPause = true;
	}

	public void switchState(int state) {
		if (mmainMenu != null) {
			mmainMenu.setVisible(false);
		}
		if (mHelp != null) {
			mHelp.setVisible(false);
		}
		// if (mLevelSubSelect != null) {
		// mLevelSubSelect.setVisible(false);
		// }
		if (mLevelSelect != null) {
			mLevelSelect.setVisible(false);
		}
		if (mIGM != null) {
			mIGM.setVisible(false);
		}
		if (mGamePlayLayer != null) {
			mGamePlayLayer.setVisible(false);
		}
		if (mLevelConfirm != null) {
			mLevelConfirm.setVisible(false);
		}
		
		if (mshop != null) {
			mshop.setVisible(false);
		}
		
		
		if(mResult != null) {
			mResult.setVisible(false);
		}

		switch (state) {
		case GS_GAME_MAIN_MENU:
			if (mmainMenu != null) {
				mmainMenu.setVisible(true);
				mmainMenu.checkSoundState();
			}
			break;
		case GS_GAME_HELP:
			if (mHelp != null) {
				mHelp.setVisible(true);
				mHelp.setEnterState(mGameStatue);
			}
			break;
		case GS_GAME_LEVEL_SELECT:
			if (mLevelSelect != null) {
				mLevelSelect.setVisible(true);
				mLevelSelect.enterSelectLevel();
			}
			if ((mGameStatue == GS_GAME_IGM || mGameStatue == GS_GAME_RESULT)
					&& mGamePlayLayer != null) {
				mGamePlayLayer.clearLevelData();
			}
			break;
		case GS_GAME_LEVEL_CONFIRM:
			if (mLevelSelect != null) {
				mLevelSelect.setVisible(true);
			}
			if (mLevelConfirm != null) {
				mLevelConfirm.setVisible(true);
				mLevelConfirm.setConfirmData();
			}
			break;
		// case GS_GAME_LEVEL_SUB:
		// if (mLevelSubSelect != null) {
		// mLevelSubSelect.setVisible(true);
		// mLevelSubSelect.enterSubLevel(mLevelSelect.getLevelSelect());
		// }
		// if (mGameStatue == GS_GAME_IGM || mGameStatue == GS_GAME_RUNNING) {
		// SoundManager.playMusic(R.raw.m_bg);
		//
		// if(mGamePlayLayer != null) {
		// mGamePlayLayer.clearLevelData();
		// }
		// }
		// break;
		case GS_GAME_IGM:
			if (mGamePlayLayer != null) {
				mGamePlayLayer.setVisible(true);
			}
			if (mIGM != null) {
				mIGM.setVisible(true);
			}
			break;
		case GS_GAME_RUNNING:
			if (mGameStatue == GS_GAME_IGM) {
				if (mGamePlayLayer != null) {
					mGamePlayLayer.setVisible(true);
				}
			} else {
				if (mGamePlayLayer != null) {
//					if (mGameStatue == GS_GAME_RUNNING) {
//						mGamePlayLayer.clearLevelData();
//					}
					mGamePlayLayer.setVisible(true);
					mGamePlayLayer.setLevelData();
					// mGamePlayLayer.checkSoundState();
				}
			}
			break;
		case GS_GAME_SHOP:
			if (mshop != null) {
				mshop.setVisible(true);
				mshop.enterShop();
			}
			break;
		case GS_GAME_RESULT:
			if (mGamePlayLayer != null) {
				mGamePlayLayer.setVisible(true);
			}
			if (mResult!= null) {
				mResult.setVisible(true);
			}
			break;
		}

		mGameStatue = state;
	}

	public void tick(float delta) {
		Global.sFrameCount++;
		if (Global.sFrameCount < 0) {
			Global.sFrameCount = 0;
		}
		// float x = getPositionX();
		// float y = getPositionY();
		// float ax = getAnchorX();
		// float ay = getAnchorY();
		// System.out.println("x = " + x);
		// System.out.println("y = " + y);
		// System.out.println("ax = " +ax);
		// System.out.println("ay = " + ay);
		switch (mGameStatue) {
		case GS_GAME_RUNNING:
			if (mPause)
				return;
			if (mGamePlayLayer != null)
				mGamePlayLayer.update(delta);
		case GS_GAME_HELP:
			if (mHelp != null) {
				mHelp.tick();
			}
			break;
		case GS_GAME_RESULT:
			if (mResult != null) {
				mResult.tick(delta);
			}
			break;
		}
	}
	
	public void unlockNext() {
		if (mLevelSelect != null) {
			mLevelSelect.unlockLevel();
		}
	}

	public void ExitAppConfirm() {
		GameInterface.exit(GateActivity.sInstance, new GameExitCallback() {
			@Override
			public void onConfirmExit() {
				GateActivity.sInstance.finish();
				Process.killProcess(Process.myPid());
			}
			@Override
			public void onCancelExit() {
				
			}
		});
//		GateActivity.sInstance.runOnUiThread(new Runnable() {
//			@Override
//			public void run() {
//				new AlertDialog.Builder(GateActivity.sInstance)
//						.setMessage("是否退出？").setNegativeButton("否", null)
//						.setPositiveButton("是", new OnClickListener() {
//							@Override
//							public void onClick(DialogInterface dialog,
//									int which) {
//								GateActivity.sInstance.finish();
//								Process.killProcess(Process.myPid());
//							}
//						}).show();
//			}
//		});
	}

//	@Override
//	public boolean wyTouchesBegan(MotionEvent event) {
//		switch (mGameStatue) {
//		case GS_GAME_RUNNING:
//			if (mIsGameStart) {
//
//			}
//			break;
//		case GS_GAME_HELP:
//			mHelp.wyTouchesBegan(event);
//			break;
//		}
//		return true;
//	}
////////////////上面是模板/////////////////////////////
	
//	int mgold;
//	public int getMgold() {
//		return mgold;
//	}
//	public void setMgold(int mgold) {
//		this.mgold = mgold;
//	}

}
